transaction.admin = liwe.module ( 'transaction.admin', null, "Transaction" );

transaction.admin._dest_div = "system-content";
transaction.admin._visiblecnt = {};
transaction.admin._opts_module = null;




transaction.admin.init = function ( dest_div )
{
	if ( ! transaction.templates )
	{
		transaction.load_templates ( function () { transaction.admin._init ( dest_div); } );
		return;
	}
	transaction.admin._init ( dest_div );
};

transaction.admin._init = function ( dest_div )
{
    if ( dest_div ) transaction.admin._dest_div =  dest_div;
    var _acts = [	{ label: 'Tipologie', func: function () { transaction.admin._kind(); } },
		        ];
    
	transaction.admin.register ( _acts );
	transaction.admin._am_kind_init ( 'list_trasaction_kind' );

	transaction.admin._get_valid_modules ( transaction.admin._cache_module );	
	
};

transaction.admin._cache_module = function ( mods, cback, force )
{

	if ( ! mods ) return;
	if ( force == true ) transaction.admin._opts_module = null;
	
	if ( transaction.admin._opts_module )
	{
		if ( cback ) cback();
		return;	
	}; 

	transaction.admin._opts_module = [];

	var get ={ 'action' : "system.ajax.list_modules" }
	transaction.ajax ( get, function ( resp )
	{
		var modules = resp.modules
		liwe.types.to_list ( modules );
		modules.iterate ( function ( v )
		{
			if ( mods.indexOf ( v ) != -1 ) 
			{
				var str_label = v +  ".admin.label";
				var label =  eval ( str_label );
				transaction.admin._opts_module.push ( { 'label' : label, 'value' : v } );
			};			
		} );
		if ( cback ) cback();
	} );

	return;
}

transaction.admin._get_valid_modules = function ( cback )
{
	var call_cback = function ( resp )
	{
		if ( cback ) cback ( resp )
		return;
	}

	var post = {  'action' : 'transaction.admin.get_valid_modules' };
	transaction.ajax ( post, function ( resp )
	{
		call_cback ( resp.mods )
	}, function ( ) { call_cback ( list() ) } );
	
}


transaction.admin._kind = function ()
{
	$( transaction.admin._dest_div, transaction.templates [ 'ADMIN_KIND_LAYOUT' ] );
	transaction.admin._kind_refresh();
}

transaction.admin._am_kind_init = function ( dest_div )
{
        if ( transaction.admin.am_kind ) return;
        transaction.admin.am_kind = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'am_tr_kind',
                                        'list_action': "transaction.ajax.list_kind",
                                        'mod_menu_actions': [ 
																{ func: "transaction.admin.add_kind()", 'class' : 'add' , label: 'Aggiungi Tipologia' }
															]
                                } );

        transaction.admin.am_kind.set_row_fields (
                                [
                                        [ "id", "id_kind" ],
                                        [ "Visible", "_visible" ],
                                        [ "Nome", "name" ],
                                        [ "Charge", "_charge" ],
                                        [ "Modulo", "module" ]
                                ]
                                );

        transaction.admin.am_kind.cbacks [ 'row_manip' ] = transaction.admin._rwman_kind;
        transaction.admin.am_kind.cbacks [ 'del_item' ]  = function ( id ) { transaction.admin.del_kind ( id ); };
        transaction.admin.am_kind.cbacks [ 'edit_item' ]  = function ( id ) { transaction.admin.edit_kind ( id ); };
        transaction.admin.am_kind.cbacks [ 'noresult' ] = function () { transaction.admin.add_kind (); };
};

transaction.admin._rwman_kind = function ( ds, row )
{
    row [ "_id" ] = row [ 'id_kind' ];
	var _switch =  [ 'off', 'on' ];
	var visible = row [ 'visible' ];
	row [ '_visible' ] = String.formatDict ( transaction.templates [ 'ADMIN_ON_OFF'], { '_class' : _switch [ visible ] , '_id' : row [ '_id' ] } );
    var _dct = { '_class' : 'money' };
    _dct [ '_txt' ] ='&euro; ' +  Money.fromLongInt ( row [ 'charge' ] ) ;
    row [ "_charge" ] = String.formatDict ( system.templates [ 'admin_man_col' ] , _dct );

};

transaction.admin._kind_refresh =  function ( cback )
{
    transaction.admin.am_kind.show_list ();
	transaction.admin._kind_form ();
	if ( cback ) cback();
};

transaction.admin.add_kind = function ()
{
	transaction.admin._kind_form ();
};

transaction.admin.edit_kind = function ( id )
{
        var get = { action : 'transaction.ajax.get_kind', id_kind : id };
        transaction.ajax ( get , function ( resp )
        {  
			var data = resp.get ( 'kind', {} ); 
			transaction.admin._kind_form ( data  );
			$ ( "tr_kind_title", "Modifica Tipologia [ " + id + " ]" );
        });
};

transaction.admin._kind_form = function ( data )
{
	if ( typeof ( data ) == 'undefined' ) data = {};
    var f = new liwe.form.instance ( 'tr_kind_form' );
	f.events [ 'complete' ] = transaction.admin._kind_save;
	f.hidden ( "action", 'transaction.admin.set_kind' );
	f.hidden ( 'id_kind', data.get ( 'id_kind', 0 )  );
	f.select ( { label: 'Modulo', name: 'module', options: transaction.admin._opts_module, value : data.get ( 'module' ) } );
	f.select ( { label : 'Modalit&agrave;', name : 'mode', options : transaction.options, value : data.get ( 'mode' ) , mandatory : true } );
	f.text ( { 'name' : 'name', 'label' : "Nome",  'size' : 20, 'maxlength' : 50, mandatory : true, 'value' : data.get ( 'name', '' ) } );
	f.money ( { 'label' : 'Costo' , 'name' : 'charge' , size : 10, 'maxlength' : 10, 'value' : data.get ( 'charge' , 0 ) } );
	f.jconed ( { label: 'Descrizione', name: "descr",  width: 400, height: 200, 'value' : data.get ( 'descr', '' )  } );
	f.checkbox ( { label: 'Visibile', name: 'visible', checked : data.get ( 'visible' , 0 ) ,  value : 1 } );
	f.submit ( { value: 'Salva', 'class' : "button green",nonl : true } );
	f.button ( { value: 'Annulla', 'class' : "grey", onclick : "transaction.admin._kind_refresh()" } );
	f.set ( 'tr_kind_form' );
	$ ( "tr_kind_title", "Nuova Tipologia" );
};


transaction.admin._kind_save =  function ()
{
	transaction.admin._kind_refresh ( function () 
	{
		liwe.utils.notifier.show ( 'Tipologia Salvata correttamente', 'info', 2000)
	});	
};


transaction.admin.set_kind_visible = function ( el, id  )
{
	var visible = 1;
	var new_class = "on"
	var old_class = "off"
	if ( liwe.dom.has_class ( el, "on" ) ) 
	{
		visible = 0;
		new_class = "off";
		old_class = "on";
	}
	var post = { 'action' : 'transaction.admin.set_kind_visible',
				 'ids' : id, 
				 'visible' : visible }
	transaction.ajax ( post , function ()
	{
		var  el_onoff = $el ( "visible_" + id );
		liwe.dom.del_class ( el_onoff, old_class );
		liwe.dom.add_class ( el_onoff, new_class );		
	} );

}
